package com.ly.report_ah.sercive.master.impl;

import com.github.pagehelper.PageHelper;
import com.ly.report_ah.dao.master.ReportMapper;
import com.ly.report_ah.sercive.master.DatService;
import com.ly.report_ah.util.ConstatUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ly.report_ah.util.ConstatUtil.*;

@Service
public class DatServiceImpl implements DatService {
    //污染模板
    public static String POLLUTION_TEMPLATE = "${creepageDistanceDesc}${oldInsulatorDesc}" + "判定该线路" + RED_START + "绝缘水平下降" + RED_END + "。";
    //风害模板
    public static String WIND_TEMPLATE = "该线路穿越" + UNDERLINE_START + "${regionCodes}" + UNDERLINE_END + "级风区，最高等级的风区标准为" + UNDERLINE_START + "${standardWindSpeed}" + UNDERLINE_END + "。处在该最高等级级" + UNDERLINE_START + "${regionMaxCode}" + UNDERLINE_END + "风区的杆塔有" + UNDERLINE_START + "${wirePoleNames}" + UNDERLINE_END + "号杆塔，编号为" + UNDERLINE_START + "${wirePoleNOs}" + UNDERLINE_END + "，设计风速为" + UNDERLINE_START + "${windMaxSpeed}" + UNDERLINE_END + "，设计风速均小于标准值，判定该线路存在" + RED_START + "风害" + RED_END + "。";
    //老旧模板
    public static String OLD_TEMPLATE = "该线路上的" + UNDERLINE_START + "${wirePoleNames}" + UNDERLINE_END + "号杆塔，编号为" + UNDERLINE_START + "${wirePoleNOs}" + UNDERLINE_END + "，其投运时间分别为" + UNDERLINE_START + "${runDate}" + UNDERLINE_END + "，应退役时间分别为" + UNDERLINE_START + "${expirationDate}" + UNDERLINE_END + "超过服役期限，判定该线路存在" + RED_START + "倒杆风险" + RED_END + "。";


    @Resource
    private ReportMapper reportMapper;

    /**
     * @description 风害
     * @author: GuoZheng
     * @create 2021-01-17 20:41
     */
    @Override
    public Map<String, String> getWindWirePoleMapByObjId(String objId) {

        //得到跨越的风区等级集合
        List<String> regionCodeList = reportMapper.getWindRegionCodesDistinctByObjId(objId);
        if (CollectionUtils.isEmpty(regionCodeList)) {
            return Map.of();//如果没有数据返回空
        }
        //得到最高等级的风区等级 regionCode为数字，可以强转为数字
        String regionMaxCode = regionCodeList.stream().max(Comparator.comparing(Integer::parseInt)).orElse("");

        //得的处于最高等级的前三个杆塔
        PageHelper.startPage(1, 3);//分页取前三条
        List<Map<String, Object>> wirePoleList = reportMapper.getWindWirePoleByObjId(objId, regionMaxCode);

        //构造成需要的字段的map
        return buildWindWirePoleMap(wirePoleList, regionMaxCode, getRegionCodesDesc(regionCodeList));
    }

    /**
     * @description 得到风害杆塔map
     * @author: GuoZheng
     * @create 2021-01-17 22:36
     */
    private Map<String, String> buildWindWirePoleMap(List<Map<String, Object>> wirePoleList, String regionMaxCode, String regionCodesDesc) {
        Map<String, Object> firstWirePole = wirePoleList.get(0);
        String standardWindSpeed = firstWirePole.get("WIND_MAX_SPEED").toString();
        String lineName = firstWirePole.get("LINE_NAME").toString();


        String wirePoleNames = wirePoleList.stream()
                .map(o -> o.get("NAME").toString())
                .collect(Collectors.joining("，"));

        String wirePoleNOs = wirePoleList.stream()
                .map(o -> o.get("WIREPOLE_NO").toString())
                .collect(Collectors.joining("，"));

        String windSpeed = wirePoleList.stream()
                .map(o -> o.get("WIND_SPEED") + "m/s")
                .collect(Collectors.joining("，"));

        return Map.of(
                "lineName", lineName,
                "regionMaxCode", regionMaxCode,
                "wirePoleNames", wirePoleNames,
                "wirePoleNOs", wirePoleNOs,
                "standardWindSpeed", standardWindSpeed,
                "windSpeed", windSpeed,
                "regionCodes", regionCodesDesc
        );
    }

    /**
     * @description 绝缘子水平下降的明细
     * @author: GuoZheng
     * @create 2021-01-17 20:40
     */
    @Override
    public Map<String, String> getPollutionsInsulatorMapByObjId(String objId) {

        //得到跨越的污区等级集合
        List<String> regionCodeList = reportMapper.getPollutionRegionCodesDistinctByObjId(objId);
        if (CollectionUtils.isEmpty(regionCodeList)) {
            return Map.of();//如果没有数据返回空
        }
        //得到最高等级的污区等级
        String regionMaxCode = regionCodeList.stream().max(Comparator.comparing(Integer::parseInt)).orElse("");

        //得的处于最高等级且爬电比距不合格的前两个个杆塔
        PageHelper.startPage(1, 1);//分页取第一条
        List<Map<String, Object>> insulatorList = reportMapper.getPollutionInsulatorByObjId(objId, regionMaxCode);

        //得的处于老旧的绝缘子
        PageHelper.startPage(1, 1);//分页取第一条
        List<Map<String, Object>> oldInsulatorList = reportMapper.getOldInsulatorByObjId(objId);
        String lineName = "";
        String creepageDistanceDesc = "";
        if (!CollectionUtils.isEmpty(insulatorList)) {
            String standardDistance = insulatorList.get(0).get("WQBZ").toString() + "mm/kV";
            lineName = insulatorList.get(0).get("LINE_NAME").toString();
            creepageDistanceDesc = buildCreepageDistanceDesc(insulatorList,
                    regionMaxCode,
                    getRegionCodesDesc(regionCodeList),
                    standardDistance
            );
        }

        String oldInsulatorDesc = "";
        if (!CollectionUtils.isEmpty(oldInsulatorList)) {
            lineName = oldInsulatorList.get(0).get("LINE_NAME").toString();
            oldInsulatorDesc = buildOldInsulatorDesc(oldInsulatorList);
        }

        //构造成需要的字段的map
        return Map.of(
                "lineName", lineName,
                "creepageDistanceDesc", creepageDistanceDesc,
                "oldInsulatorDesc", oldInsulatorDesc);
    }

    /**
     * @description 构造绝缘子爬电比距不合格描述
     * @author: GuoZheng
     * @create 2021-01-19 14:47
     */
    private String buildCreepageDistanceDesc(List<Map<String, Object>> insulatorList, String regionMaxCode, String regionCodesDesc, String standardDistance) {


        String wirePoleNames = insulatorList.stream()
                .map(o -> o.get("WIREPOLE_NAME").toString())
                .collect(Collectors.joining("，"));

        String wirePoleNOs = insulatorList.stream()
                .map(o -> o.get("WIREPOLE_NO").toString())
                .collect(Collectors.joining("，"));

        //存在一些绝缘子没有名称，此处处理为，没有名称使用model 2021-1-17 23:36:29 郭征
        String insulatorNames = insulatorList.stream()
                .map(o -> o.get("NAME") != null ? o.get("NAME").toString() : o.get("MODEL").toString())
                .collect(Collectors.joining("，"));

        String creepageDistance = insulatorList.stream()
                .map(o -> o.get("PDBJ") + "mm/kV")
                .collect(Collectors.joining("，"));

        String template = "该线路穿越" + UNDERLINE_START + "%s" + UNDERLINE_END + "级污区，最高等级的污区爬电比距标准为" + UNDERLINE_START + "%s" + UNDERLINE_END + "。处在最高等级" + UNDERLINE_START + "%s" + UNDERLINE_END + "级污区的杆塔有" + UNDERLINE_START + "%s" + UNDERLINE_END + "号杆塔，编号为" + UNDERLINE_START + "%s" + UNDERLINE_END + "，其不合格绝缘子为" + UNDERLINE_START + "%s" + UNDERLINE_END + "，不合格绝缘子的爬电比距为" + UNDERLINE_START + "%s" + UNDERLINE_END + "，小于标准值。";
        return String.format(template, regionCodesDesc, standardDistance, regionMaxCode, wirePoleNames, wirePoleNOs, insulatorNames, creepageDistance);
    }

    /**
     * @description 构造老旧绝缘子描述
     * @author: GuoZheng
     * @create 2021-01-18 22:14
     */
    private String buildOldInsulatorDesc(List<Map<String, Object>> oldInsulatorList) {


        String wirePoleNames = oldInsulatorList.stream()
                .map(o -> o.get("WIREPOLE_NAME").toString())
                .collect(Collectors.joining("，"));

        String wirePoleNOs = oldInsulatorList.stream()
                .map(o -> o.get("WIREPOLE_NO").toString())
                .collect(Collectors.joining("，"));

        //存在一些绝缘子没有名称，此处处理为，没有名称使用model 2021-1-17 23:36:29 郭征
        String insulatorNames = oldInsulatorList.stream()
                .map(o -> o.get("NAME") != null ? o.get("NAME").toString() : o.get("MODEL").toString())
                .collect(Collectors.joining("，"));

        String runDate = oldInsulatorList.stream()
                .map(o -> (Date) o.get("TYSJ"))
                .map(ConstatUtil::formatDate)
                .collect(Collectors.joining("，"));

        String expirationDate = oldInsulatorList.stream()
                .map(o -> (Date) o.get("TUIYISJ"))
                .map(ConstatUtil::formatDate)
                .collect(Collectors.joining("，"));
        //污染子模板
        String POLLUTION_CHILD_TEMPLATE = "该线路存在复合绝缘子投运年限大于15年，老化造成的防污能力下降的复合绝缘子所属杆塔为" + UNDERLINE_START + "%s" + UNDERLINE_END + "号杆塔，杆塔编号为" + UNDERLINE_START + "%s" + UNDERLINE_END + "，绝缘子名称为" + UNDERLINE_START + "%s" + UNDERLINE_END + "，绝缘子投运时间为" + UNDERLINE_START + "%s" + UNDERLINE_END + "，应退役时间为" + UNDERLINE_START + "%s" + UNDERLINE_END + "。";
        return String.format(POLLUTION_CHILD_TEMPLATE, wirePoleNames, wirePoleNOs, insulatorNames, runDate, expirationDate);
    }

    /**
     * @description 老旧
     * @author: GuoZheng
     * @create 2021-01-17 20:41
     */
    @Override
    public Map<String, String> getOldWirePoleMapByObjId(String objId) {
        //得的前三个杆塔
        PageHelper.startPage(1, 3);//分页取前三条
        List<Map<String, Object>> oldWirePoleList = reportMapper.getOldWirePoleByObjId(objId);

        if (CollectionUtils.isEmpty(oldWirePoleList)) {
            return Map.of();//如果没有数据返回空
        }

        //构造成需要的字段的map
        return buildOldWirePoleMap(oldWirePoleList);
    }

    /**
     * companyName供电公司lineName线路上的wirePoleNames号杆塔，编号为wirePoleNOs，其投运时间分别为runDate,应退役时间分别为expirationDate,超过服役期限，判定该线路存在倒杆风险。
     *
     * @description 得到老旧杆塔的map
     * @author: GuoZheng
     * @create 2021-01-17 22:16
     */
    private Map<String, String> buildOldWirePoleMap(List<Map<String, Object>> oldWirePoleList) {
        Map<String, Object> firstOldWirePole = oldWirePoleList.get(0);
        String lineName = firstOldWirePole.get("LINE_NAME").toString();
        String wirePoleNames = oldWirePoleList.stream()
                .map(o -> o.get("NAME").toString())
                .collect(Collectors.joining("，"));

        String wirePoleNOs = oldWirePoleList.stream()
                .map(o -> o.get("WIREPOLE_NO").toString())
                .collect(Collectors.joining("，"));

        String runDate = oldWirePoleList.stream()
                .map(o -> (Date) o.get("TYSJ"))
                .map(ConstatUtil::formatDate)
                .collect(Collectors.joining("，"));

        String expirationDate = oldWirePoleList.stream()
                .map(o -> (Date) o.get("TUIYISJ"))
                .map(ConstatUtil::formatDate)
                .collect(Collectors.joining("，"));
        return Map.of(
                "lineName", lineName,
                "wirePoleNames", wirePoleNames,
                "wirePoleNOs", wirePoleNOs,
                "runDate", runDate,
                "expirationDate", expirationDate
        );
    }

    /**
     * @description 得到跨域区域等级描述，并根据等级正序排序
     * @author: GuoZheng
     * @create 2021-01-19 15:43
     */
    private String getRegionCodesDesc(List<String> regionCodeList) {
        return regionCodeList.stream().sorted(Comparator.comparing(Integer::parseInt)).collect(Collectors.joining("，"));
    }


}
